import {my64Encode, dataDecode} from "../crypto";

export * from './ajax'
export * from './fetch'
export * from './url'

export const getHttpKey = function () {
	const a = Math.abs(~~(0xc34f * Math.random()))
	const b = Math.abs(Date.now() % 0xc34f)
	const c = Math.abs((a ^ b) - (b >> 3) - (a >> 7))
	return my64Encode(a.toString(16) + b.toString(16).padStart(4, '0') + c.toString(16).padStart(4, '0'))
}

export const responseDecode = function (res) {
	try {
		const data = JSON.parse(dataDecode(res.data))
		return {code: res.code, msg: res.msg, data}
	} catch (e) {
		return {code: res.code, msg: res.msg, data: res.data}
	}
}

export function download(filename, blob) {
	if (!blob) return
	const url = window.URL.createObjectURL(new Blob([blob]))
	const link = document.createElement('a')
	link.style.display = 'none'
	link.href = url
	link.setAttribute('download', filename)
	document.body.appendChild(link)
	link.click()
	window.URL.revokeObjectURL(url) // 下载完成进行释放
	document.body.removeChild(link)
}
